<?php

namespace Gds\VenteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Gds\VenteBundle\Entity\SessionCaisse;
use Gds\VenteBundle\Form\SessionCaisseType;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Ps\PdfBundle\Annotation\Pdf;
use Gds\VenteBundle\Entity\ReglementClient;
use Gds\VenteBundle\Form\ReglementClientType;

class JournalCaisseController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $caisses = $em->getRepository('GdsVenteBundle:Caisse')->findBy(array('departement' => $this->getUser()->getDepartement(), 'is_active' => true));

        return $this->render('GdsVenteBundle:JournalCaisse:index.html.twig', array('caisses' => $caisses));
    }

    public function detailAction() {
        $em = $this->getDoctrine()->getEntityManager();
        $request = $this->getRequest();
        $date1 = $request->get('date1');
        $date2 = $request->get('date2');
        $heure1 = $request->get('heure1');
        $heure2 = $request->get('heure2');
        $caisse = $request->get('caisse');
        $reglements = $em->getRepository('GdsVenteBundle:ReglementClient')->getDetailJournal($caisse, $date1, $date2, $heure1, $heure2);
        $reglementsArray = array();
        $ttc = 0;
        $espece = 0;
        $cheque = 0;
        $effet = 0;
        foreach ($reglements as $k => $reglement) {
            $ttc = $ttc + $reglement->getMontantTtc();
            $reglementArray = array();
            $reglementArray['date'] = $reglement->getDate();
            $reglementArray['heure'] = $reglement->getTime();
            $reglementArray['montant'] = $reglement->getMontantTtc();
            $reglementArray['lignes'] = array();
            $reglementArray['lignes']['espece'] = 0;
            $reglementArray['lignes']['cheque'] = 0;
            $reglementArray['lignes']['effet'] = 0;
            foreach ($reglement->getLignes() as $k => $ligne) {
                if ($ligne->getType() == 'Espece') {
                    $reglementArray['lignes']['espece'] = $reglementArray['lignes']['espece'] + $ligne->getMontant();
                    $espece = $espece + $ligne->getMontant();
                }
                if ($ligne->getType() == 'Cheque') {
                    $reglementArray['lignes']['cheque'] = $reglementArray['lignes']['cheque'] + $ligne->getMontant();
                    $cheque = $cheque + $ligne->getMontant();
                }
                if ($ligne->getType() == 'Effet') {
                    $reglementArray['lignes']['effet'] = $reglementArray['lignes']['effet'] + $ligne->getMontant();
                    $effet = $effet + $ligne->getMontant();
                }
            }
            $reglementsArray[] = $reglementArray;
        }
        $viw = $this->renderView('GdsVenteBundle:JournalCaisse:detail.html.twig', array('reglements' => $reglementsArray));
        $result = array();
        $result['list'] = $viw;
        $result['ttc'] =$ttc;
        $result['cheque'] =$cheque;
        $result['effet'] =$effet;
        $result['espece'] =$espece;
        return new Response(json_encode($result));
    }

}
